package com.example.managemyself.data.database;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.managemyself.data.model.AIMessage;

import java.util.List;

/**
 * AI消息DAO
 */
@Dao
public interface AIMessageDao {
    
    @Insert
    long insert(AIMessage message);
    
    @Update
    void update(AIMessage message);
    
    @Delete
    void delete(AIMessage message);
    
    @Query("SELECT * FROM ai_messages WHERE conversationId = :conversationId ORDER BY timestamp ASC")
    LiveData<List<AIMessage>> getMessagesByConversation(int conversationId);
    
    @Query("SELECT * FROM ai_messages WHERE conversationId = :conversationId ORDER BY timestamp ASC")
    List<AIMessage> getMessagesByConversationSync(int conversationId);
    
    @Query("SELECT * FROM ai_messages WHERE id = :messageId")
    LiveData<AIMessage> getMessageById(int messageId);
    
    @Query("DELETE FROM ai_messages WHERE conversationId = :conversationId")
    void deleteByConversation(int conversationId);
    
    @Query("SELECT COUNT(*) FROM ai_messages WHERE conversationId = :conversationId")
    LiveData<Integer> getMessageCount(int conversationId);
    
    @Query("SELECT * FROM ai_messages WHERE conversationId = :conversationId ORDER BY timestamp DESC LIMIT 1")
    AIMessage getLastMessage(int conversationId);
    
    @Query("SELECT * FROM ai_messages WHERE conversationId = :conversationId AND content LIKE :searchQuery ORDER BY timestamp ASC")
    LiveData<List<AIMessage>> searchMessages(int conversationId, String searchQuery);
} 